package cn.jiyun.service.impl;

import cn.jiyun.common.R;
import cn.jiyun.dao.DishDao;
import cn.jiyun.dao.DishFlavorDao;
import cn.jiyun.pojo.Cd;
import cn.jiyun.pojo.Dish;
import cn.jiyun.pojo.DishFlavor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class DishService implements cn.jiyun.service.DishService {
    @Resource
    private DishDao dishDao;

    @Resource
    private DishFlavorDao dishFlavorDao;


//    @Resource
//    private CategoryDao categoryDao;

//    分页查询
    @Override
    public R page(Page<Dish> page1, String name) {
//        QueryWrapper<Dish> qw=new QueryWrapper<>();
//        qw.eq(name!=null,"name",name);
//        Page<Dish> dishPage = dishDao.selectPage(page1, qw);
        Page<Cd> dishPage = dishDao.selectPagezx(page1, name);
        return R.success(dishPage);
    }

    @Override
    public void del( Long id,Long idc) {
//        Dish dish=new Dish();
//        dish.setId(id);
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setUpdateUser(idc);
      dishDao.deleteById(id);


    }

    @Override
    public List<Dish> list(Long categoryId) {
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);

        return dishDao.selectList(qw);
    }


    //状态修改
    @Override
    public void status0(Long id) {
        Integer status=0;
//        dish.setUpdateTime(LocalDateTime.now());
        dishDao.update0(id,status);
    }

//    状态修改
    @Override
    public void status1(Long id) {

        Integer status=1;
//        dish.setUpdateTime(LocalDateTime.now());

        dishDao.update1(id,status);

    }
//添加
    @Override
    public Object add(Long id, Cd cd) {
        List<DishFlavor> flavors = cd.getFlavors();
        cd.setUpdateUser(id);
        cd.setCreateUser(id);
        cd.setCreateTime(LocalDateTime.now());
        cd.setUpdateTime(LocalDateTime.now());
        Long cid= Long.valueOf(dishDao.insert(cd));
        System.out.println(cid);
        for (DishFlavor flavor : flavors) {
            flavor.setUpdateUser(id);
            flavor.setCreateUser(id);
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setDishId(cd.getId());
            dishFlavorDao.insert(flavor);
        }
        return R.success("成功");
    }

//    回显
    @Override
    public Cd hx(Long id) {
        Cd dish = dishDao.selectByIddi(id);
        List<DishFlavor>  df=dishFlavorDao.selectByIddid(id);
        dish.setFlavors(df);
        BeanUtils.copyProperties("小","大");
        return dish;
    }
    //    修改
    @Override
    public Object update(Cd cd, Long id) {
//先修改主表
            cd.setUpdateUser(id);
            cd.setUpdateTime(LocalDateTime.now());
            dishDao.updateById(cd);
//            创建副标
            List<DishFlavor> flavors = cd.getFlavors();

//            填写副标要删除的
//            LambdaUpdateWrapper<DishFlavor> qw=new LambdaUpdateWrapper<>();
//            qw.eq(DishFlavor::getDishId,cd.getId());
//            dishFlavorDao.delete(qw) ; 异变逻辑删

            dishFlavorDao.deleteah(cd.getId());

            for (DishFlavor flavor : flavors) {
                flavor.setUpdateUser(id);
                flavor.setCreateUser(id);
                flavor.setCreateTime(LocalDateTime.now());
                flavor.setUpdateTime(LocalDateTime.now());
                flavor.setDishId(cd.getId());
                dishFlavorDao.insert(flavor);
            }

       return R.success("成功");

    }





}
